package com.tianqu.tms.www.car.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.CommercialVehicle;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.Vendor;
import com.tianqu.tms.www.car.viewhelper.CarCreationInputViewHelper;

/**
 * @author zhangxueliang
 * 高级搜索中车辆修改追加初始Action
 */
public class CarSearchUpdateInputAction extends TmsAction{

	private Logger logger = Logger.getLogger(CarSearchUpdateInputAction.class);
	private static final String VIN = "vin";
	private static final String SEARCHFLAG = "searchFlag";
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
//		CarSearchUpdateInputController controller = new CarSearchUpdateInputController();
//		controller.execute(uc);
		CarCreationInputViewHelper carCreationInputViewHelper = new CarCreationInputViewHelper();
		carCreationInputViewHelper = (CarCreationInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		Codes codes = Codes.getInstance();
		if("0".equals(carCreationInputViewHelper.getExchangeFlag())){
			carCreationInputViewHelper.setAttribute(String.valueOf(codes.getItemCodeByItemIdAndItemValue(Codes.ATTACHMENT_PROPERTY,carCreationInputViewHelper.getAttribute())));		
			carCreationInputViewHelper.setCarType(String.valueOf(codes.getItemCodeByItemIdAndItemValue(Codes.VEHICLE_TYPE,carCreationInputViewHelper.getCarType())));
			carCreationInputViewHelper.setCarUse(String.valueOf(codes.getItemCodeByItemIdAndItemValue(Codes.USER_NATURE,carCreationInputViewHelper.getCarUse())));
			carCreationInputViewHelper.setColor(String.valueOf(codes.getItemCodeByItemIdAndItemValue(Codes.VEHICLE_COLOR,carCreationInputViewHelper.getColor())));
			carCreationInputViewHelper.setCountryStyle(String.valueOf(codes.getItemCodeByItemIdAndItemValue(Codes.VEHICLE_COUNTRY,carCreationInputViewHelper.getCountryStyle())));
			carCreationInputViewHelper.setFuelType(String.valueOf(codes.getItemCodeByItemIdAndItemValue(Codes.FUEL_TYPE,carCreationInputViewHelper.getFuelType())));
			carCreationInputViewHelper.setGearbox(String.valueOf(codes.getItemCodeByItemIdAndItemValue(Codes.GEARBOX_TYPE,carCreationInputViewHelper.getGearbox())));
			if(carCreationInputViewHelper.getStatus() == 5){
				carCreationInputViewHelper.setVinFlag(1);
				carCreationInputViewHelper.setOwnerTypeFlag(1);
				carCreationInputViewHelper.setOwnerTypeD(carCreationInputViewHelper.getOwnerType());
				carCreationInputViewHelper.setCardTypeS(carCreationInputViewHelper.getCardType());
			//add by huangheli at20150816 start
			} else if(carCreationInputViewHelper.getStatus() == 1){
				carCreationInputViewHelper.setVinFlag(1);				
				carCreationInputViewHelper.setOwnerTypeFlag(2);
			}//add by huangheli at20150816 end
			else{
				carCreationInputViewHelper.setVinFlag(2);				
				carCreationInputViewHelper.setOwnerTypeFlag(2);
				
			}
			carCreationInputViewHelper.setOwnerType(String.valueOf(codes.getItemCodeByItemIdAndItemValue(Codes.CAR_OWNER, carCreationInputViewHelper.getOwnerType())));
			carCreationInputViewHelper.setCardType(String.valueOf(codes.getItemCodeByItemIdAndItemValue(Codes.PLATE_TYPE,carCreationInputViewHelper.getCardType())));
		}
		
		if("0".equals(carCreationInputViewHelper.getExchangeFlag())){
			carCreationInputViewHelper.setExchangeFlag("1");
		}
		if(request.getParameter(SEARCHFLAG)!=null && !"".equals(request.getParameter(SEARCHFLAG)) 
				&& carCreationInputViewHelper.getFlushFlag() == null)
		{
			Member mem = Member.getInstance();
			carCreationInputViewHelper.setMemberId(String.valueOf(mem.getMemIdByMemName(carCreationInputViewHelper.getMemberId())));
			carCreationInputViewHelper.setSearchFlag("1");
			carCreationInputViewHelper.setFlushFlag("2");
		}
		Codes code = Codes.getInstance();
		carCreationInputViewHelper.setCarTypeList(code
				.getCodeslistByItemId(Codes.VEHICLE_TYPE));// 车辆类型
		carCreationInputViewHelper.setColorList(code
				.getCodeslistByItemId(Codes.VEHICLE_COLOR));// 车辆颜色
		carCreationInputViewHelper.setCarUseList(code
				.getCodeslistByItemId(Codes.USER_NATURE));// 使用性质list
		carCreationInputViewHelper.setFuelTypeList(code
				.getCodeslistByItemId(Codes.FUEL_TYPE));// 燃料种类list
		carCreationInputViewHelper.setCountryStyleList(code
				.getCodeslistByItemId(Codes.VEHICLE_COUNTRY));// 国别list
		carCreationInputViewHelper.setAttributeList(code
				.getCodeslistByItemId(Codes.ATTACHMENT_PROPERTY));// 车辆状态list'
		carCreationInputViewHelper.setOwnerTypeList(code
				.getCodeslistByItemId(Codes.CAR_OWNER));// 车主类型list
		carCreationInputViewHelper.setCardTypeList(code
				.getCodeslistByItemId(Codes.PLATE_TYPE));// 号牌种类list
		
		Member member = Member.getInstance();
		carCreationInputViewHelper.setMemberList(member.findAllMemInfo());// 会员类型
		CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
		// 车辆品牌
		Vendor vendor = Vendor.getInstance();
		uc.setAttribute(VIN, carCreationInputViewHelper.getVin());
		if(!StringUtil.isNullOrBlank(carCreationInputViewHelper.getVendorId())){
			if("V".equals(carCreationInputViewHelper.getVendorId().substring(0,1))){
				carCreationInputViewHelper.setFromType(2);
				carCreationInputViewHelper.setVendorList(commercialVehicle.findAllVendorInfo());
			}else if("S".equals(carCreationInputViewHelper.getVendorId().substring(0,1))){
				carCreationInputViewHelper.setFromType(1);
				carCreationInputViewHelper
				.setVendorList(vendor.findAllVendorInfo());
			}
		}
		
		
		carCreationInputViewHelper.setRemark(carCreationInputViewHelper.getRemark());//WL:20150509
		carCreationInputViewHelper.setGearBoxList(code.getCodeslistByItemId(Codes.GEARBOX_TYPE));//变速箱类型list
		request.setAttribute(VIEWHELP, carCreationInputViewHelper);
		logger.debug(this.getClass().getName() + "#doAction was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
